package com.example.huichaoliu;

import java.util.ArrayList;
import java.util.List;

import com.huichaoliu.adapters.ViewPagerAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.UserInfo;
import com.huichaoliu.stylist.activity.LoginStylistActivity;
import com.rederxu.tools.DisplayUtils;
import com.rederxu.views.RoundImageView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class WelcomeActivity extends Activity implements OnClickListener {

	private ViewPager viewPager = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		
		UserInfo info = new UserInfo(WelcomeActivity.this);
		if(info.isFirstLogin()){
			viewPager = (ViewPager) findViewById(R.id.viewPager);
			List<View> listView = new ArrayList<View>();
			ImageView img1 = new ImageView(this);
			ImageView img2 = new ImageView(this);
			ImageView img3 = new ImageView(this);
			ImageView img4 = new ImageView(this);
			img1.setScaleType(ScaleType.FIT_XY);
			img2.setScaleType(ScaleType.FIT_XY);
			img3.setScaleType(ScaleType.FIT_XY);
			img4.setScaleType(ScaleType.FIT_XY);
			img1.setImageResource(R.drawable.welcome1);
			img2.setImageResource(R.drawable.welcome2);
			img3.setImageResource(R.drawable.welcome3);
			img4.setImageResource(R.drawable.welcome);
			listView.add(img1);
			listView.add(img2);
			listView.add(img3);
			listView.add(img4);
			viewPager.setAdapter(new ViewPagerAdapter(listView));
			initProgressLayout(0);
			viewPager.setOnPageChangeListener(new OnPageChangeListener() {
				
				@Override
				public void onPageSelected(int selection) {
					initProgressLayout(selection);
				}
				
				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onPageScrollStateChanged(int arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			viewPager.setOnTouchListener(new OnTouchListener() {
				
				private float yActionDown = 0.0f;
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if(event.getAction() == MotionEvent.ACTION_DOWN){
						yActionDown = event.getY();
					}else if(event.getAction() == MotionEvent.ACTION_UP){
						if(Math.abs(event.getY() - yActionDown) < 10){
							if(event.getY() < DisplayUtils.getScreenHeight(WelcomeActivity.this) / 2){
								View view = new View(WelcomeActivity.this);
								view.setId(R.id.v_top);
								WelcomeActivity.this.onClick(view);
							}else{
								View view = new View(WelcomeActivity.this);
								view.setId(R.id.v_bottom);
								WelcomeActivity.this.onClick(view);
							}
						}
					}
					return false;
				}
			});
		}
		info.setFirstLogin(false);
		info.commit();
		findViewById(R.id.v_top).setOnClickListener(this);
		findViewById(R.id.v_bottom).setOnClickListener(this);
	}
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			Class activity = (Class)msg.obj;
			if(viewPager != null && viewPager.getCurrentItem() == 3){
				startActivity(new Intent(WelcomeActivity.this,activity));
				finish();
			}
			if(viewPager == null){
				startActivity(new Intent(WelcomeActivity.this,activity));
				finish();
			}
			super.handleMessage(msg);
		}
	};
	
	private void initProgressLayout(int selection){
		LinearLayout progressLayout = (LinearLayout) findViewById(R.id.progressLayout);
		progressLayout.removeAllViews();
		for(int i = 0; i < 4; i++){
			RoundImageView riv = new RoundImageView(this);
			if(i == selection){
				riv.setImageResource(R.drawable.mark_banner_selected);
			}else{
				riv.setImageResource(R.drawable.mark_banner_unselected);
			}
			progressLayout.addView(riv);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
			lp.width = DisplayUtils.dipToPx(this, 8);
			lp.height = lp.width;
			lp.setMargins(lp.width, 0, 0, 0);  
			riv.setLayoutParams(lp);
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.v_bottom:
			Message msg = new Message();
			msg.obj = LoginStylistActivity.class;	//在这里写搭配师首页
			handler.sendMessage(msg);
//			Toast.makeText(getApplicationContext(), "搭配师页面", 1).show();
			break;
		case R.id.v_top:
			msg = new Message();
			msg.obj = HomeActivity.class;
			handler.sendMessage(msg);
			break;
		}
	}
}
